<?php
	require_once("config.php");
	
    if ($_POST['action'] == "delete") {
        if (!isset($_POST['listingid']))
            die("No Listing Specified.");
            
        $query = "DELETE FROM listings WHERE id=$_POST[listingid];";
        DBM::Execute($query);
        
        $query = "DELETE FROM listing_data WHERE listingid=$_POST[listingid];";
        DBM::Execute($query);
        
        header("Location: listings.php");
    }
    else if ($_GET['action'] == "copy") {
        // use listing manager to copy a listing
        $newlisting_id = ListingManager::CopyListing($_GET['listingid']);
        header("Location: listing.php?action=edit&id=$newlisting_id");
    }
    else if ($_GET['action'] == "delete") {
        Functions::PrintHeader("Delete Listing");
        if (!isset($_GET['listingid'])) {
            ErrorMessage::Show("No Listing","You didn't specify a listing to delete");
        }
        else {
            $listing = ListingManager::FetchListing($_GET['listingid']);
            if (is_null($listing)) {
                ErrorMessage::Show("Invalid Listing","You didn't specify a valid listing to delete");
            }
            else {
                echo "<form method='POST' action='listings.php'>";
                echo "<input type='hidden' name='action' value='delete' />";
                echo "<input type='hidden' name='listingid' value='$_GET[listingid]' />";
                
                WarningMessage::Show("Are you sure?","Are you sure you want to delete the listing at <b>" . $listing->GetAddress() . "</b>" .
                    "<hr />" . 
                    "<input type='reset' value='No' />" .
                    "<input type='submit' value='Yes' />" . 
                    "<br /><br />"
                );
                
                echo "</form>";
            }
        }
        Functions::PrintFooter();
    }
    else if ($_GET['action'] == 'unlist') {
        $array = array(
            "listed" => 0,
            "availableon" => 0
        );
        DBM::Execute(QueryTools::CreateQuery($array,"listings","update",$_GET['id']));
        header("Location: listings.php");
    }
    else if ($_GET['action'] == 'list') {
        $_POST['availableon'] = strtotime($_POST['leasedate_' . $_POST['id']]);
        
        echo "<pre>" . print_r($_POST,true) . "</pre>";
        $array = array(
            "listed" => 1,
            "availableon" => $_POST['availableon']
        );
        DBM::Execute(QueryTools::CreateQuery($array,"listings","update",$_POST['id']));
        header("Location: listings.php");
    }
    else {
        $menu = new Menu("Actions");
        $menu->AddItem(new MenuItem("Add Listing","listing.php"));
        MenuManager::AddMenu("Your Listings", $menu);
        
        Functions::PrintHeader("Your Listings");

        if (!AuthManager::GetUserID()) {
            ErrorMessage::Show("Permissions Error","You are not logged in.  You must be logged in to view listings.");
            Functions::PrintFooter();
            exit;
        }
        else if (!AuthManager::CheckPermissions(array(AuthConstants::ADMINISTRATORS, AuthConstants::LANDLORDS))) {
            ErrorMessage::Show("Permissions Error","You must be a landlord to use this page.");
            Functions::PrintFooter();
            exit;
        }
        
        $query = "SELECT * FROM listings WHERE landlordid=" . AuthManager::GetUserID() . " AND listed = 1";
        $listings = DBM::FetchAllRows($query);

        echo "<h2>Listings Available for Rent</h2>";
        
        if (count($listings) == 0) {
            InfoMessage::Show("No Listings", "You don't have any listings currently available for rent.  Create one with the link at the left or make one of your unlisted listings available.");
        }
        else {
            echo "<table class='wide cgltable'>";
            echo "<thead><tr>" .
                    "<th style='width:100px;'>Listing ID</th>" .
                    "<th>Address</th>" .
                    "<th>&nbsp;</th>" .
                "</tr></thead>";
            foreach ($listings as $listing) {
                echo "<tr>";
                $listingobj = ListingManager::FetchListing($listing['id']);
                echo "<td>" . $listingobj->GetID() . "</td>" .
                        "<td>" . $listingobj->GetAddress() . "</td>" .
                        "<td>" . 
                        "<a href='listing.php?action=view&id=" . $listingobj->GetID() . "'>view</a> &#9679; " .
                        "<a href='listing.php?action=edit&id=" . $listingobj->GetID() . "'>edit</a> &#9679; " .
                        "<a href='listings.php?action=copy&listingid=" . $listingobj->GetID() . "'>copy</a> &#9679; " . 
                        "<a href='photos.php?id=" . $listingobj->GetID() . "'>manage photos</a> " . 
                        "<hr />" . 
                        "<a href='listings.php?action=delete&listingid=" . $listingobj->GetID() . "'>delete</a> &#9679; " .
                        "<a href='listings.php?action=unlist&id=" . $listingobj->GetID() . "'>unlist (make private)</a>" .
                        "</td>";
                echo "</tr>";
            }
            
            echo "</table>";
        }
        
        echo "<br />";
        echo "<br />";
        echo "<h2>Unlisted/Private Listings</h2>";

        $query = "SELECT * FROM listings WHERE landlordid=" . AuthManager::GetUserID() . " AND listed = 0;";
        $listings = DBM::FetchAllRows($query);
    
        if (count($listings) == 0) {
            InfoMessage::Show("No Listings", "You don't have any unlisted listings.  Create new listings with the link at the left or on the menu bar.");
        }   
        else {
            echo "<div style='font-size:8pt;'>When you make a listing public you also need to specify when the lease starts.  Every time you re-list an address, you will need to reset this field.</div><br />";
            echo "<table class='wide cgltable'>";
            echo "<thead><tr>" .
                    "<th style='width:100px;'>Listing ID</th>" .
                    "<th>Address</th>" .
                    "<th>&nbsp;</th>" .
                    "<th>List</th>" . 
                "</tr></thead>";
            foreach ($listings as $listing) {
                echo "<tr>";
                $listingobj = ListingManager::FetchListing($listing['id']);
                echo "<td>" . $listingobj->GetID() . "</td>" .
                        "<td>" . $listingobj->GetAddress() . "</td>" .
                        "<td>" . 
                        "<a href='listing.php?action=view&id=" . $listingobj->GetID() . "'>view</a> &#9679; " .
                        "<a href='listing.php?action=edit&id=" . $listingobj->GetID() . "'>edit</a> &#9679; " .
                        "<a href='listings.php?action=copy&listingid=" . $listingobj->GetID() . "'>copy listing</a> &#9679; " . 
                        "<a href='photos.php?id=" . $listingobj->GetID() . "'>manage photos</a>" . 
                        "<hr />" . 
                        "<a href='listings.php?action=delete&listingid=" . $listingobj->GetID() . "'>delete listing</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " .
                        "</td>";
                 echo "<td valign='middle'>" . 
                        "<form method='post' action='listings.php?action=list'><input type='hidden' name='id' value='" . $listingobj->GetID() . "' />" .
                        "<input type='hidden' id='leasedate_" . $listingobj->GetID() . "' name='leasedate_" . $listingobj->GetID() . "' />" . 
                        "<span style='display:inline-block; vertical-align:middle;'>List with lease starting on: <span id='leasedate_label_" . $listingobj->GetID() . "'>" . date("M/d/y") . "</span></span>" . 
                        "<br /><input type='button' id='trigger_" . $listingobj->GetID() . "' value='Change Lease Start Date' />" . 
                        "<input type='submit' value='List' />" .
                        "</form>";
                  echo Functions::CalendarInit(
                        "trigger_" . $listingobj->GetID(),
                        "leasedate_" . $listingobj->GetID(),
                        "leasedate_label_" . $listingobj->GetID()
                  );
                            
                        "</td>";
                echo "</tr>";
            }   
    
            echo "</table>";
        }   

        
        Functions::PrintFooter();
    }
?>
